package com.example.xuanke.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xuanke.Entity.CourseEntity;
import com.example.xuanke.Entity.StudentCourseEntity;
import com.example.xuanke.commton.BusinessException;
import com.example.xuanke.mapper.StudentCourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentCourseService extends ServiceImpl<StudentCourseMapper, StudentCourseEntity> {

    @Autowired
    private CourseService courseService;

    /**
     * 学生选课
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 是否选课成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean enrollCourse(Long studentId, Long courseId) {
        // 检查是否已经选过这门课
        QueryWrapper<StudentCourseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId)
                    .eq("course_id", courseId);
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException("您已经选过这门课程");
        }

        // 获取课程信息
        CourseEntity course = courseService.getById(courseId);
        if (course == null) {
            throw new BusinessException("课程不存在");
        }

        // 检查课程是否已满
        if (course.getCurrentEnrollment() >= course.getMaxCapacity()) {
            throw new BusinessException("课程已满，无法选课");
        }

        // 使用新方法更新课程选课人数
        int newEnrollment = course.getCurrentEnrollment() + 1;
        boolean updateResult = courseService.updateCourseEnrollment(courseId, newEnrollment);
        if (!updateResult) {
            throw new BusinessException("选课失败，请稍后重试");
        }

        // 添加选课记录
        StudentCourseEntity studentCourse = new StudentCourseEntity();
        studentCourse.setStudentId(studentId);
        studentCourse.setCourseId(courseId);
        return this.save(studentCourse);
    }

    /**
     * 学生退课
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 是否退课成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean dropCourse(Long studentId, Long courseId) {
        // 检查是否选过这门课
        QueryWrapper<StudentCourseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId)
                    .eq("course_id", courseId);
        StudentCourseEntity studentCourse = this.getOne(queryWrapper);
        if (studentCourse == null) {
            throw new BusinessException("您未选择这门课程");
        }

        // 获取课程信息
        CourseEntity course = courseService.getById(courseId);
        if (course == null) {
            throw new BusinessException("课程不存在");
        }

        // 使用新方法更新课程选课人数
        if (course.getCurrentEnrollment() > 0) {
            int newEnrollment = course.getCurrentEnrollment() - 1;
            boolean updateResult = courseService.updateCourseEnrollment(courseId, newEnrollment);
            if (!updateResult) {
                throw new BusinessException("退课失败，请稍后重试");
            }
        }

        // 删除选课记录
        return this.remove(queryWrapper);
    }
} 